package com.zhengsr.wanandroid.mvp.present;

import com.zhengsr.corelib.bean.ArticleCollectBean;
import com.zhengsr.corelib.bean.BaseResponse;
import com.zhengsr.corelib.utils.RxUtils;
import com.zhengsr.wanandroid.mvp.base.BasePresent;
import com.zhengsr.wanandroid.mvp.base.IBaseView;
import com.zhengsr.wanandroid.mvp.contract.ContractManager;
import com.zhengsr.wanandroid.mvp.model.DataManager;
import com.zhengsr.wanandroid.widget.CusSubscribe;

/**
 * Created by zhengshaorui
 * time: 2018/10/7
 */

public class CollectPresent extends BasePresent<IBaseView> {
    private IBaseView mView;
    private DataManager mDataManager;
    private int mCurrentPage = 0;
    public static CollectPresent create(IBaseView view) {
        return new CollectPresent(view);
    }

    private CollectPresent(IBaseView view) {
        mView = view;
        mDataManager =  DataManager.getInstance();
    }

    /**
     * 获取收藏列表
     */
    public void getCollectList(){
        addSubscribe(mDataManager.getCollectList(mCurrentPage)
                .compose(RxUtils.<BaseResponse<ArticleCollectBean>>rxScheduers())
                .compose(RxUtils.<ArticleCollectBean>handleResult())
                .subscribeWith(new CusSubscribe<ArticleCollectBean>(mView) {
                    @Override
                    public void onNext(ArticleCollectBean articleListPageBean) {
                        if (mView instanceof ContractManager.ICollectView){
                            ContractManager.ICollectView view = (ContractManager.ICollectView) mView;
                            view.getCollectData(articleListPageBean.datas);
                        }

                    }
                })
        );
    }

    /**
     * 收藏站内文章
     */
    public void addCollectArticle(int articleId){

        addSubscribe(
                mDataManager.addCollectArticle(articleId)
                        .compose(RxUtils.<BaseResponse<ArticleCollectBean>>rxScheduers())
                        .subscribeWith(new CusSubscribe<BaseResponse<ArticleCollectBean>>(mView) {
                            @Override
                            public void onNext(BaseResponse<ArticleCollectBean> articleCollectBean) {

                                if (mView instanceof ContractManager.IAddOrCancelCollect){
                                    ContractManager.IAddOrCancelCollect view = (ContractManager.IAddOrCancelCollect) mView;
                                    view.addCollectArticleSuccess(articleCollectBean);
                                }
                            }
                        })
        );
    }

    //取消站内文章
    public void cancelCollectArticle(int articleId){

        addSubscribe(
                mDataManager.cancelCollectArticle(articleId)
                        .compose(RxUtils.<BaseResponse<BaseResponse>>rxScheduers())
                        .subscribeWith(new CusSubscribe<BaseResponse<BaseResponse>>(mView) {
                            @Override
                            public void onNext(BaseResponse<BaseResponse> baseResponseBaseResponse) {
                                if (mView instanceof ContractManager.IAddOrCancelCollect){
                                    ContractManager.IAddOrCancelCollect view = (ContractManager.IAddOrCancelCollect) mView;
                                    view.cancelCollectArticleSuccess(baseResponseBaseResponse);
                                }
                            }
                        })
        );
    }
}
